package com.quan.system.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.quan.common.biz.support.BaseService;
import com.quan.system.entity.SystemArea;
import com.quan.system.service.SystemAreaService;

/**
 * <p>
 * 地区表 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2018-12-06
 */
@Service
public class SystemAreaServiceImpl extends BaseService<SystemArea> implements SystemAreaService {
	
private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Async
	@Override
	public int updateLocation(SystemArea area) {
		return super.updateSelective(area) ;
	}
	
	@Async
	@Override
	public void asyncGeneratorVantAreaJson() {
		Map<String, Object> province = new HashMap<String, Object>() ;
		Map<String, Object> city = new HashMap<String, Object>() ;
		Map<String, Object> county = new HashMap<String, Object>() ;
		
		SystemArea chooseProvince = new SystemArea() ;
		chooseProvince.setLevel(1);
		List<SystemArea> selectAll = super.selectAll(chooseProvince) ;
		for (SystemArea area : selectAll) {
			province.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		SystemArea chooseCity = new SystemArea() ;
		chooseCity.setLevel(2);
		List<SystemArea> selectAllCity = super.selectAll(chooseCity) ;
		for (SystemArea area : selectAllCity) {
			city.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		SystemArea chooseCounty = new SystemArea() ;
		chooseCounty.setLevel(3);
		List<SystemArea> selectAllCounty = super.selectAll(chooseCounty) ;
		for (SystemArea area : selectAllCounty) {
			county.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		Map<String, Object> exportdefault = new HashMap<String, Object>() ;
		exportdefault.put("province_list", province);
		exportdefault.put("city_list", city);
		exportdefault.put("county_list", county);
		
		try {
			String path = "d:/" ;
			InputStream inputStream = IOUtils.toInputStream(JSON.toJSONString(exportdefault), "UTF-8") ;
			IOUtils.copy(inputStream, new FileOutputStream(path + "Area.json"));
			logger.debug("输出路径：" + path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}